package com.miao.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.miao.base.exception.ValidGroups;
import com.miao.dto.*;
import com.miao.base.model.PageParams;
import com.miao.base.model.PageResult;
import com.miao.pojo.CourseBase;
import com.miao.service.ICourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

//@Api(value = "课程内容相关接口", tags = "课程内容相关接口")
@Slf4j
@RestController
@RequestMapping("/content")
public class CourseBaseController {
    @Autowired
    private ICourseBaseService iCourseBaseService;

    //课程分页查询
    @PreAuthorize("hasAuthority('xc_teachmanager_course_base')")
    //@PreAuthorize("hasAuthority('fuck')")
    @PostMapping("/course/list")
    public PageResult<CourseBase> list(PageParams pageParams, @RequestBody(required = false)ContentCondition contentCondition){
        //获取公司ID
        String principal = (String)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        XcUserExt xcUserExt = JSON.parseObject(principal, XcUserExt.class);
        String _companyId = xcUserExt.getCompanyId();
        long companyId = Long.parseLong(_companyId);


        log.info("进行课程内容分页以及条件查询！！");
        Long pageNo = pageParams.getPageNo();
        Long pageSize = pageParams.getPageSize();
        Page<CourseBase> courseBasePage = new Page<>(pageNo, pageSize);
        //添加条件
        LambdaQueryWrapper<CourseBase> courseBaseLambdaQueryWrapper = new LambdaQueryWrapper<CourseBase>();
        courseBaseLambdaQueryWrapper.eq(CourseBase::getCompanyId, companyId);
        if(null != contentCondition) {
            courseBaseLambdaQueryWrapper.like(null != contentCondition.getCourseName(), CourseBase::getName, contentCondition.getCourseName());
            courseBaseLambdaQueryWrapper.like(null != contentCondition.getAuditStatus(), CourseBase::getAuditStatus, contentCondition.getAuditStatus());
            courseBaseLambdaQueryWrapper.like(null != contentCondition.getPublishStatus(), CourseBase::getStatus, contentCondition.getPublishStatus());
        }
        //分页查询
        Page<CourseBase> page = iCourseBaseService.page(courseBasePage, courseBaseLambdaQueryWrapper);

        return new PageResult<CourseBase>(
            page.getRecords(), page.getTotal(), pageNo, pageSize
        );
    }

    //课程添加
    @PostMapping("/course")
    public CourseBaseInfoDto addBaseContent(@RequestBody @Validated(ValidGroups.Insert.class) AddCourseDto addCourseDto){
        //获取机构id，还没实现，先瞎写一个
        Long comId = 1232141425L;

        return iCourseBaseService.addBase(comId, addCourseDto);
    }

    //用户修改课程信息时候，先根据课程id查询出信息在前端展示
    @GetMapping("/course/{id}")
    public CourseBaseInfoDto getCourseById(@PathVariable Long id){
        return iCourseBaseService.getCourseInfoDto(id);
    }

    //课程修改
    @PutMapping("/course")
    public CourseBaseInfoDto editContent(@RequestBody @Validated(ValidGroups.Update.class) EditCourseDto editCourseDto){
        //获取机构id，还没实现，先瞎写一个
        Long comId = 1232141425L;

        return iCourseBaseService.updateCourse(comId, editCourseDto);
    }

    //删除课程
    @DeleteMapping("/course/{id}")
    public void deleCourse(@PathVariable Long id){
        //获取机构id，还没实现，先瞎写一个
        Long comId = 1232141425L;
        iCourseBaseService.deleteCourse(comId, id);
    }




}
